package com.mark.maven.controller;

import com.mark.maven.controller.dto.CreateOrderDTO;
import com.mark.maven.domain.Address;
import com.mark.maven.domain.Contact;
import com.mark.maven.domain.Customer;
import com.mark.maven.domain.Order;
import com.mark.maven.service.order.create.CreateOrderService;
import com.mark.maven.service.order.create.req.CreateOrderRequest;
import com.mark.maven.service.order.create.resp.CreateOrderResponse;
import com.mark.maven.service.order.query.QueryOrderService;
import com.mark.maven.service.order.query.req.QueryOrderRequest;
import com.mark.maven.service.order.query.resp.QueryOrderResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    private final CreateOrderService createOrderService;
    private final QueryOrderService queryOrderService;

    public OrderController(
            CreateOrderService createOrderService,
            QueryOrderService queryOrderService) {
        this.createOrderService = createOrderService;
        this.queryOrderService = queryOrderService;
    }

    @PostMapping("/create")
    public CreateOrderResponse<String> createOrder(@RequestBody CreateOrderDTO createOrderDTO) {
        CreateOrderRequest request = toRequest(createOrderDTO);
        return createOrderService.createOrder(request);
    }

    @GetMapping("/query/{orderNo}")
    public QueryOrderResponse<Order> createOrder(@PathVariable("orderNo") String orderNo) {
        return queryOrderService.queryOrder(QueryOrderRequest.builder().orderNo(orderNo).build());
    }

    private CreateOrderRequest toRequest(CreateOrderDTO createOrderDTO) {
        return CreateOrderRequest.builder()
                .customer(Customer.builder()
                        .customerNo(createOrderDTO.getCustomerNo())
                        .customerName(createOrderDTO.getCustomerName())
                        .build())
                .sender(Contact.builder()
                        .name(createOrderDTO.getSenderName())
                        .mobilePhone(createOrderDTO.getSenderPhone())
                        .telephone(createOrderDTO.getSenderPhone())
                        .address(Address.builder().detailAddress(createOrderDTO.getSenderAddress()).build())
                        .build())
                .receiver(Contact.builder()
                        .name(createOrderDTO.getReceiverName())
                        .mobilePhone(createOrderDTO.getReceiverPhone())
                        .telephone(createOrderDTO.getReceiverPhone())
                        .address(Address.builder().detailAddress(createOrderDTO.getReceiverAddress()).build())
                        .build())
                .build();
    }
}